package org.chen.utils;

import org.chen.model.dto.ArticleDTO;
import org.chen.model.elastic.ArticleIndex;
import org.chen.model.mongodb.ArticleDocument;
import org.chen.model.vo.ArticleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class ArticleConverter {

    // DTO -> MongoDB文档
    public static ArticleDocument toDocument(ArticleDTO dto) {
        if (dto == null) return null;

        ArticleDocument document = new ArticleDocument();
        document.setTitle(dto.getTitle());
        document.setAuthor(dto.getAuthor());
        document.setContentHtml(dto.getContentHtml());
        document.setCategory(dto.getCategory());
        document.setTags(dto.getTags());
        document.setThumbnailUrl(dto.getThumbnailUrl());
        document.setMainImageUrl(dto.getMainImageUrl());
        document.setStatus(dto.getStatus());
        document.setIsTop(dto.getIsTop());
        document.setSummary(dto.getSummary());

        // 设置一些默认值
        if (document.getCreateTime() == null) {
            document.setCreateTime(new Date());
        }
        document.setUpdateTime(new Date());

        return document;
    }

    // MongoDB文档 -> ES索引
    public static ArticleIndex toIndex(ArticleDocument document) {
        if (document == null) return null;

        return ArticleIndex.builder()
                .id(document.getId())
                .title(document.getTitle())
                .author(document.getAuthor())
                .category(document.getCategory())
                .tags(document.getTags())
                .thumbnailUrl(document.getThumbnailUrl())
                .source(document.getSource())
                .status(document.getStatus())
                .isTop(document.getIsTop())
                .isHot(document.getIsHot())
                .isRecommend(document.getIsRecommend())
                .summary(document.getSummary())
                .viewCount(document.getViewCount())
                .createTime(document.getCreateTime())
                .build();
    }

    // MongoDB文档 -> VO
    public static ArticleVO toVO(ArticleDocument document) {
        if (document == null) return null;

        return ArticleVO.builder()
                .id(document.getId())
                .title(document.getTitle())
                .author(document.getAuthor())
                .category(document.getCategory())
                .tags(document.getTags())
                .thumbnailUrl(document.getThumbnailUrl())
                .mainImageUrl(document.getMainImageUrl())
                .source(document.getSource())
                .status(document.getStatus())
                .isTop(document.getIsTop())
                .viewCount(document.getViewCount())
                .createTime(document.getCreateTime())
                .updateTime(document.getUpdateTime())
                .build();
    }

    public static ArticleVO toVOForEdit(ArticleDocument document) {
        if (document == null) return null;

        return ArticleVO.builder()
                .id(document.getId())
                .title(document.getTitle())
                .author(document.getAuthor())
                .category(document.getCategory())
                .tags(document.getTags())
                .thumbnailUrl(document.getThumbnailUrl())
                .mainImageUrl(document.getMainImageUrl())
                .source(document.getSource())
                .status(document.getStatus())
                .summary(document.getSummary())
                .isTop(document.getIsTop())
                .contentHtml(document.getContentHtml())
                .viewCount(document.getViewCount())
                .createTime(document.getCreateTime())
                .updateTime(document.getUpdateTime())
                .build();
    }

    // ES索引 -> VO
    public static ArticleVO toVOFromIndex(ArticleIndex index) {
        if (index == null) return null;

        return ArticleVO.builder()
                .id(index.getId())
                .title(index.getTitle())
                .author(index.getAuthor())
                .category(index.getCategory())
                .tags(index.getTags())
                .thumbnailUrl(index.getThumbnailUrl())
                .source(index.getSource())
                .topOrder(index.getTopOrder())
                .hotOrder(index.getHotOrder())
                .recommendOrder(index.getRecommendOrder())
                .status(index.getStatus())
                .isTop(index.getIsTop())
                .isHot(index.getIsHot())
                .isRecommend(index.getIsRecommend())
                .viewCount(index.getViewCount())
                .createTime(index.getCreateTime())
                .build();
    }
}
